﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using BCL;
using UploadingControl.SilverlightService;

namespace UploadingControl
{
    public partial class NewListPage : UserControl
    {
        public NewListPage()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(NewListPage_Loaded);
        }

        void NewListPage_Loaded(object sender, RoutedEventArgs e)
        {
            Bind();
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            OpenDetailPage(PageMode.Add, null);
        }

        private void btnRefresh_Click(object sender, RoutedEventArgs e)
        {
            datePicker1.SelectedDate = null;
            PageNumber = 0;
            Bind();
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            Bind();
        }

        void Bind()
        {
            SilverlightServiceClient client = new SilverlightServiceClient();
            busyIndicator1.IsBusy = true;
            client.SearchCompleted += (sender, arg) =>
                {
                    dataGrid1.ItemsSource = arg.Result;
                    busyIndicator1.IsBusy = false;
                    if (arg.Result.Count != 0)
                        DoPaging(PAGESIZE, arg.Result[0].TotalCount);
                    else
                        stackPanelPager.Children.Clear();
                };
            int fromRecordNumber = PageNumber * PAGESIZE + 1;
            int toRecordNumber = fromRecordNumber + PAGESIZE - 1;
            client.SearchAsync(datePicker1.SelectedDate, null, fromRecordNumber, toRecordNumber);

        }
        const int PAGESIZE = 10;

        /// <summary>
        /// Add buttons to the StackPanel and onclick these buttons will take to the respective page number as button Content Property
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="totalRow"></param>
        public void DoPaging(int pageSize, int totalRow)
        {
            //count the total number of pages
            int totalpage = Convert.ToInt32(Math.Ceiling(totalRow / (double)pageSize));
            stackPanelPager.Children.Clear();
            if (totalpage > 1)
            {
                if (stackPanelPager != null)
                {
                    //clear all the previous controls from stackpanel
                    //stackPanelPager.Children.Clear();
                    for (int i = 1; i <= totalpage; i++)
                    {
                        HyperlinkButton btn = new HyperlinkButton();
                        btn.Width = 20;
                        btn.Content = i;
                        if (PageNumber + 1 == i)
                        {
                            btn.FontSize = 12.0;
                            btn.IsEnabled = false;
                            btn.FontWeight = FontWeights.ExtraBold;
                        }
                        //add the click event to the button
                        btn.Click += new RoutedEventHandler(OnPageIndex);
                        btn.Margin = new Thickness(3);
                        // add button to the StackPanel
                        stackPanelPager.Children.Add(btn);
                    }
                }
            }
        }
        public int PageNumber { get; set; }
        /// <summary>
        /// Handles the click event of buttons having page numbers
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void OnPageIndex(object sender, RoutedEventArgs e)
        {
            HyperlinkButton btn = sender as HyperlinkButton;
            int iPageNumber = Convert.ToInt32(btn.Content.ToString());
            //int PageNumber = iPageNumber * PAGESIZE;
            PageNumber = iPageNumber - 1;

            Bind();
        }
        void GetById(PageMode mode)
        {
            SilverlightServiceClient client = new SilverlightServiceClient();
            busyIndicator1.IsBusy = true;
            client.GetCompleted += (sender, arg) =>
            {

                busyIndicator1.IsBusy = false;
                OpenDetailPage(mode, arg.Result);
            };

            var logoInformation = dataGrid1.SelectedItem as LogoInformation;
            client.GetAsync(logoInformation.Id);
        }


        void OpenDetailPage(PageMode mode, LogoInformation logoinformation)
        {
            StackPanel panel = ((StackPanel)this.Parent);
            panel.Children.Clear();
            panel.Children.Add(new NewsDetailPage(mode, logoinformation));
        }

        private void hbtnView_Click(object sender, RoutedEventArgs e)
        {
            GetById(PageMode.View);
        }

        private void hbtnEdit_Click(object sender, RoutedEventArgs e)
        {
            GetById(PageMode.Edit);
        }

        private void hbtnActivate_Click(object sender, RoutedEventArgs e)
        {
            SilverlightServiceClient client = new SilverlightServiceClient();
            busyIndicator1.IsBusy = true;
            client.ActivateCompleted += (s, arg) =>
                {
                    MessageBox.Show("The news has been activated.");
                    Bind();
                };

            client.InactivateCompleted += (s, arg) =>
                {
                    MessageBox.Show("The news has been inactivated.");
                    Bind();
                };

            var activateButton = sender as HyperlinkButton;
            if (activateButton != null)
            {
                var logoInformation = dataGrid1.SelectedItem as LogoInformation;
                if (Convert.ToString(activateButton.Content) == "Activate")
                {
                    client.ActivateAsync(logoInformation.Id);
                }
                else
                {
                    client.InactivateAsync(logoInformation.Id);
                }
            }
        }

        private void hbtnPreview_Click(object sender, RoutedEventArgs e)
        {

        }

        private void dataGrid1_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            var logo = e.Row.DataContext as LogoInformation;
            StackPanel panel = dataGrid1.Columns[1].GetCellContent(e.Row) as StackPanel;

            if (panel != null)
            {

                var activateButton = panel.FindName("hbtnActivate") as HyperlinkButton;

                if (logo != null && activateButton != null)
                {
                    if (logo.IsActive)
                    {
                        activateButton.Content = "Inactivate";
                    }
                    else
                    {
                        activateButton.Content = "Activate";
                    }
                }
            }
        }
    }
}
